%% The requirements on the OTP version come mainly from the patches
%% for OTP modules - see `otp_patches/` - requiring the version of
%% certain OTP applications, e.g. `mnesia`, to be well known in order
%% for such patches to be applied deterministically.
%%
%% The expected patched OTP modules required for the OTP release are
%% copied explicitly one by one as part of the `relx` overlay
%% configuration (in this `rebar.config`), in order to prevent
%% silently skipping patches e.g. because not built.
%%
%% If the user has a version of OTP that satisfies the specified
%% required minimum OTP version and that includes OTP applications
%% with version different (e.g. greater) than the well known ones
%% (e.g. because of a new OTP version with a higher-versioned OTP
%% application e.g. `mnesia`), then the patched OTP modules are not
%% built (!) though the explicit copying in the `relx` overlay fails.
%% Addressing such a failure consists of updating the content of
%% `otp_patches/`.
{minimum_otp_vsn, "20.1"}.

{erl_opts, [debug_info, {parse_transform, lager_transform},
            {lager_extra_sinks, [epoch_mining, epoch_pow_cuckoo,
                                 epoch_metrics, epoch_sync]}]}.

{deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager.git",
                      {ref, "69b4ada"}}}, % tag: 3.6.7
        {mnesia_rocksdb, ".*", {git, "https://github.com/aeternity/mnesia_rocksdb.git", {ref,"ad8e7b6"}}},
        {enacl, ".*", {git, "https://github.com/jlouis/enacl.git", {ref,"885662c"}}},
        {enoise, "1.0.0"},
        {cowboy, "2.2.2", {git, "https://github.com/ninenines/cowboy.git", {tag, "2.2.2"}}},
        {jesse, {git, "https://github.com/tolbrino/jesse.git",
                {ref, "b0a3bae"}}}, % tag 1.5.2 + http_uri patch
        {gproc, "0.6.1"},
        {jobs, "0.9.0"},
        {exometer_core, "1.5.7"},
        {poolboy, "1.5.1"},
        {yamerl, "0.7.0"},
        {base58, {git, "https://github.com/aeternity/erl-base58.git", {ref,"60a3356"}}},
        {eper, "0.99.1"},
        {erlexec, ".*", {git, "https://github.com/saleyn/erlexec.git", {ref, "97a5188"}}},
        {sha3, {git, "https://github.com/szktty/erlang-sha3", {ref, "dbdfd12"}}},
        {sext, {git, "https://github.com/uwiger/sext.git", {ref, "615eebc"}}},
        {idna, {git, "https://github.com/aeternity/erlang-idna", {tag, "6.0.0"}}}
       ]}.

{plugins, [{swagger_endpoints, {git, "https://github.com/aeternity/swagger_endpoints", {tag, "0.2.1"}}}]}.
{swagger_endpoints, [{src, "config/swagger.yaml"}, {dst, "apps/aeutils/src/endpoints.erl"}]}.

{relx, [{release, { epoch, "version value comes from VERSION" },
         % sasl is required for the command `epoch versions` to work,
         % it is disabled in `sys.config` though.
         [runtime_tools, sasl, lager, setup, sext, rocksdb, mnesia_rocksdb, gproc,
          jobs, {mnesia, load}, exometer_core, ranch, aecore, aehttp, enacl, enoise,
          aebytecode, aevm, aechannel, aesophia]},
        {sys_config, "./config/sys.config"},
        {vm_args, "./config/vm.args"},

        {dev_mode, true},
        {include_erts, false},

        {overlay, [{copy, "REVISION", "REVISION"},
                   {copy, "VERSION" , "VERSION"},
                   {copy, "docs/installation.md" , "docs/installation.md"},
                   {copy, "docs/configuration.md" , "docs/configuration.md"},
                   {copy, "docs/operation.md" , "docs/operation.md"},
                   {copy, "docs/release-notes" , "docs/release-notes"},
                   {mkdir, "data/aecore/.genesis"},
                   {copy, "data/aecore/.genesis/accounts.json", "data/aecore/.genesis/accounts.json"},
                   {copy, "data/aecore/.genesis/accounts_test.json", "data/aecore/.genesis/accounts_test.json"},
                   {copy, "data/aecore/.genesis/messages.json", "data/aecore/.genesis/messages.json"},
                   {copy, "hooks/pre_start.sh", "bin/hooks/pre_start.sh"},
                   {copy, "apps/check_config/priv/extensions/check_config.sh", "bin/extensions/check_config"},
                   {copy, "apps/aeutils/priv/extensions/keys_gen.sh", "bin/extensions/keys_gen"},
                   {copy, "apps/aeutils/priv/extensions/get_peer_key.sh", "bin/extensions/peer_key"},
                   {copy, "apps/aeutils/priv/extensions/export_chain.sh", "bin/extensions/export_chain"},
                   {copy, "apps/aeutils/priv/extensions/messages_hash.sh", "bin/extensions/messages_hash"},
                   {copy, "otp_patches/ebin/mnesia.beam"       , "patches/ebin/mnesia.beam"       },
                   {copy, "otp_patches/ebin/mnesia_index.beam" , "patches/ebin/mnesia_index.beam" }
                  ]},

        {extended_start_script, true},
        {extended_start_script_hooks, [
          {pre_start, [{custom, "hooks/pre_start.sh"}]}
        ]},
        {extended_start_script_extensions, [
              {check_config, "extensions/check_config"},
              {keys_gen, "extensions/keys_gen"},
              {peer_key, "extensions/peer_key"},
              {export, "extensions/export_chain"},
              {messages_hash, "extensions/messages_hash"}
        ]}]
}.

{profiles, [{local, [{relx, [{dev_mode, true},
                             {include_erts, false},
                             {include_src, true}]}]
            },
            {dev1, [{relx, [{dev_mode, false},
                            {include_erts, false},
                            {sys_config, "./config/dev1/sys.config"},
                            {vm_args, "./config/dev1/vm.args"},
                            {overlay, [{mkdir, "{{output_dir}}/data"},
                                       {copy,
                                        "_build/dev1/bin/check_config",
                                        "{{output_dir}}/bin/check_config"},
                                       {copy,
                                       "_build/dev1/lib/aeutils/priv/epoch_config_schema.json",
                                       "{{output_dir}}/data/epoch_config_schema.json"}]}
                           ]},
                     {erl_opts, [{d, 'TEST'}]}
                    ]
            },
            {test, [{relx, [{dev_mode, true},
                            {include_erts, false},
                            {include_src, true},
                            {sys_config, "./config/dev1/sys.config"},
                            {vm_args, "./config/dev1/vm.args"}]},
                    {dist_node, [{setcookie, 'epoch_cookie'},
                                 {sname, 'epoch_ct@localhost'}]},
                    {deps, [{meck, "0.8.12"},
                            {websocket_client, ".*", {git,
                            "git://github.com/aeternity/websocket_client",
                            {ref, "a4fb3db"}}}
                           ]}
                   ]},
            {prod, [{relx, [{dev_mode, false},
                            {include_erts, true},
                            {include_src, false},
                            {overlay, [{copy,
                                        "_build/prod/bin/check_config",
                                        "bin/check_config"},
                                       {copy,
                                        "_build/prod/lib/aeutils/priv/epoch_config_schema.json",
                                        "data/epoch_config_schema.json"}]}
                           ]}
                   ]},
            {system_test, [
                {extra_src_dirs, ["system_test", "system_test/helpers", "apps/aehttp/test"]},
                {shell, [{apps, []}]},
                {deps, [
                    bbmustache,
                    {hackney, "1.14.3"},
                    {websocket_client, ".*", {git, "git://github.com/aeternity/websocket_client", {ref, "a4fb3db"}}}
                ]},
                {ct_opts, [{create_priv_dir, auto_per_tc}]}
            ]}
           ]
}.

{escript_main_app, check_config}.
{escript_name, "check_config"}.
{escript_incl_apps, [aeutils, jsx, yamerl, jesse, rfc3339]}.
{escript_shebang, "#!/usr/bin/env escript\n"}.
{escript_comment, "%%\n"}.

{overrides, [
  % We need to explicitely add lager as a compile-time dependency for
  % exometer_core, since we configure its logging to use lager.
  {add, exometer_core, [
    {deps, [{lager, ".*", {git, "https://github.com/erlang-lager/lager.git",
                          {ref, "69b4ada"}}} % tag: 3.6.7
           ]}]},
  {override, exometer_core, [
    {erl_opts, [{d,'HUT_LAGER'},
                {parse_transform, lager_transform},
                debug_info,
                fail_on_warning,
                {platform_define, "^((1[8|9])|2)", rand_module},
                {verbosity, trace}]}]},
  {override, sha3, [
    {pre_hooks, [{"(linux|darwin)", compile, "make "},
                 {"(linux|darwin)", release, "make "},
                 {"(linux|darwin)", eunit, "make "}]}]}]}.

{provider_hooks,
 [{pre, []},
  {post, [{compile, escriptize}]}]}.

{pre_hooks, [{compile, "make -C ./otp_patches all"},
             {compile, "erlc test/ct_eunit_xform.erl"} %% {ct_first_files, _} does not work
            ]}.

{post_hooks, [{"(linux|darwin|solaris|netbsd|freebsd)", compile, "rm ct_eunit_xform.beam"},
              {clean, "make -C ./otp_patches clean"}
             ]}.

{ct_opts, [
  {ct_hooks, [{cth_surefire, [{path, "../junit.xml"}]}]}
]}.

{dialyzer, [
            {warnings, [unknown]},
            {plt_apps, all_deps},
            {base_plt_apps, [erts, kernel, stdlib, crypto, mnesia]}
           ]}.
